//
//  FSFoodListChildVC.m
//  FieldStory
//
//  Created by itte on 2017/6/29.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "FSFoodListChildVC.h"
#import "FSShoppingCartData.h"
#import "IngredientsTableCell.h"

@interface FSFoodListChildVC ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation FSFoodListChildVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"IngredientsTableCell" bundle:nil] forCellReuseIdentifier:@"IngredientsTableCellID"];
    self.tableView.tableFooterView = [[UIView alloc] init];
}

-(void)reloadData
{
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count =[FSShoppingCartData sharedInstance].boxArray.count;
    NSLog(@"count: %ld",(long)count);
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    IngredientsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IngredientsTableCellID"];
    if (cell == nil) {
        cell = [[IngredientsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"IngredientsTableCellID"];
    }
    FSShopListModel *shop = [FSShoppingCartData sharedInstance].boxArray[indexPath.row];
    [cell.imgProduct sd_setImageWithURL:[NSURL URLWithString:shop.images] placeholderImage:[UIImage imageNamed:@"wx_icon"]];
    cell.lbName.text = shop.name;
    cell.lbNum.text = [NSString stringWithFormat:@"%ld",shop.payNum];
    cell.lbPrice.text = [NSString stringWithFormat:@"￥%@",shop.vip_price];
    cell.btnAdd.tag = 1000+indexPath.row;
    cell.btnDesc.tag = 2000+indexPath.row;
    cell.btnDel.tag = 3000+indexPath.row;
    [cell.btnAdd addTarget:self action:@selector(btnAddNum:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnDesc addTarget:self action:@selector(btnDesNum:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnDel addTarget:self action:@selector(btnDel:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(void)btnAddNum:(UIButton *)btn
{
    NSInteger num = 0;
    for (FSShopListModel *shop in [FSShoppingCartData sharedInstance].boxArray) {
        num += shop.payNum;
    }
    if (num == 11) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"已满箱，请结算后在选购吧！" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"确定",nil];
        [alert show];
        return ;
    }
    NSInteger inedex = btn.tag-1000;
    FSShopListModel *shop = [FSShoppingCartData sharedInstance].boxArray[inedex];
    shop.payNum++;
    [self refresh];
}

-(void)btnDesNum:(UIButton *)btn
{
    NSInteger inedex = btn.tag-2000;
    FSShopListModel *shop = [FSShoppingCartData sharedInstance].boxArray[inedex];
    shop.payNum--;
    [self refresh];
}

-(void)btnDel:(UIButton *)btn
{
    NSInteger inedex = btn.tag-3000;
    [[FSShoppingCartData sharedInstance].boxArray removeObjectAtIndex:inedex];
    [self refresh];
}

-(void)refresh
{
    [self.tableView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(refreshBoxStatus)]) {
        [self.delegate refreshBoxStatus];
    }
}

#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"buttonIndex: %ld",buttonIndex);
    if (buttonIndex == 1 && self.delegate && [self.delegate respondsToSelector:@selector(gotoPrePay)]) {
        [self.delegate gotoPrePay];
    }
}
@end
